<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/7/1 0001
 * Time: 下午 3:09
 */

namespace app\sports_admin\model;


use think\Model;
use traits\model\SoftDelete;

class Orders extends Model
{
    use SoftDelete;

    protected $autoWriteTimestamp = true;

    protected $pk = 'id';

    protected $table = 'orders';

    //关联家长表
    public function parents()
    {
        return $this->belongsTo('parents','parent_id','id')->field('id,phone');
    }

    /**
     * @params $params 分页及检索信息
     */
    public static function getList($params)
    {
        $rows = self::with('parents')->order('id','desc')
            ->where(function ($query)use($params){
                if($params['start'] && $params['end']){
                    $query->whereTime('create_time','between',["{$params['start']}","{$params['end']}"]);
                }
                if($params['type']){
                    $query->where('status',$params['type']);
                }
            })
            ->page($params['page'])->limit($params['rows'])
            ->field('id,parent_id,money,out_trade_no,type,status,create_time')->select();
        $count = self::with('parents')->where(function ($query)use($params){
            if($params['start'] && $params['end']){
                $query->whereTime('create_time','between',["{$params['start']}","{$params['end']}"]);
            }
            if($params['type']){
                $query->where('status',$params['type']);
            }
        })->count();
        return ['rows'=>$rows,'total'=>$count];
    }
}